package com.bs.service.impl;

import java.util.*;
import com.bs.bean.User;
import com.bs.service.UserService;
import com.bs.mapper.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
/*业务层：UserService接口的实现类，调用UserDao接口的实现类对数据进行相关业务等操作*/
public class UserServiceImpl implements UserService{

//	注入UserDao接口的实现类对象
	@Autowired
	UserMapper userDao;
//	登录校验
	public User login(String username, String password) {
		List<User> list = findAll(new User().setUsername(username).setPassword(password));
		if (list.size()>0) {
			return list.get(0);
		}else {
			throw new RuntimeException("用户名和密码不匹配");
		}
	}
//	根据条件查询user中的数据
	public List<User> findAll(User query) {
		// TODO Auto-generated method stub
		return findAll(query==null?new User():query,true);
	}
	
//	根据条件和模式查询user中的数据
	public List<User> findAll(User whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<User> list = userDao.selectByExample(whereWrap);
		if (whereWrap!=null) {
			Iterator<User> iterator = list.iterator();
			while (iterator.hasNext()) {
				User user = (User) iterator.next();
				if (whereWrap.getId() != null&&(!(whereWrap.getId()+"").equals(""))) {
					if (mode) {
						if (!(user.getId() + "").equals(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(user.getId() + "").contains(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getUsername() != null&&(!(whereWrap.getUsername()+"").equals(""))) {
					if (mode) {
						if (!(user.getUsername() + "").equals(whereWrap.getUsername() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(user.getUsername() + "").contains(whereWrap.getUsername() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getPassword() != null&&(!(whereWrap.getPassword()+"").equals(""))) {
					if (mode) {
						if (!(user.getPassword() + "").equals(whereWrap.getPassword() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(user.getPassword() + "").contains(whereWrap.getPassword() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getSex() != null&&(!(whereWrap.getSex()+"").equals(""))) {
					if (mode) {
						if (!(user.getSex() + "").equals(whereWrap.getSex() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(user.getSex() + "").contains(whereWrap.getSex() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getPhone() != null&&(!(whereWrap.getPhone()+"").equals(""))) {
					if (mode) {
						if (!(user.getPhone() + "").equals(whereWrap.getPhone() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(user.getPhone() + "").contains(whereWrap.getPhone() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
			} 
		}
		return list;
	}
	
//	查询user中的数据
	public List<User> findAll() {
		// TODO Auto-generated method stub
		return findAll(new User());
	}
	
//	根据主键查询user中的一条数据
	public User findById(Integer id) {
		// TODO Auto-generated method stub
		return userDao.selectByPrimaryKey(id);
	}

//	选择性添加user表中的user数据
	public int insert(User user) {
		// TODO Auto-generated method stub
		return userDao.insertSelective(user);
	}

//	根据主键更新user表中的一条记录
	public int updateById(User user) {
		// TODO Auto-generated method stub
		return userDao.updateByPrimaryKey(user);
	}

//	根据主键选择性更新user表中的user数据
	public int updateByIdSelective(User user) {
		// TODO Auto-generated method stub
		User dbUser = findById(user.getId());
		if (user.getId()==null) {
			user.setId(dbUser.getId());
		}
		if (user.getUsername()==null) {
			user.setUsername(dbUser.getUsername());
		}
		if (user.getPassword()==null) {
			user.setPassword(dbUser.getPassword());
		}
		if (user.getSex()==null) {
			user.setSex(dbUser.getSex());
		}
		if (user.getPhone()==null) {
			user.setPhone(dbUser.getPhone());
		}
		return updateById(user);
	}

//	根据条件删除user表中的数据
	public int delete(User whereWrap) {
		// TODO Auto-generated method stub
		return delete(whereWrap,true);
	}

//	根据条件和模式删除user表中的数据
	public int delete(User whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<User> list = findAll(whereWrap, mode);
		for (User user : list) {
			deleteById(user.getId());
		}
		return list.size();
	}
//	根据主键删除user表中的一条记录
	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return userDao.deleteByPrimaryKey(id);
	}

//	根据主键集合批量删除user表中的记录
	public int deleteByIds(List<Integer> list){
		for (Integer id : list) {
			deleteById(id);
		}
		return list.size();
	}
}
